﻿﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DALDWF.CustomControl;
using DALDWF.App_Code;
using BAL.Entities;
using Animaonline.WeatherAPI;
using System.IO;

namespace DALDWF
{
    public partial class frmMain : DACustomForm
    {
        public frmMain()
        {
            InitializeComponent();

            doSettingApp();

            //string dataFile = @"data.xml";
            //if(File.Exists(dataFile))
            //{
            //    MessageBox.Show("File exists!");
            //    doSettingApp();
            //}
            //else
            //{ 
            //    MessageBox.Show("File is not exists!"); 
            //}
               
            
            //MessageBox.Show(clsDateTimeExtension.CurrentDateTime().ToString());
            //MessageBox.Show(Application.ExecutablePath.ToString());
        }

        #region --- Startup methods
        void doSettingApp()
        {
            FontExist(); // Check Arial font in client machine
            SettingUpfont(); // Setting for all of controls
            SettingUp(); // Setting up for system

            doCurrentWeather(); // Get current weather
            doTommorrowWeather(); // Get Tommorrow weather
            doNextTommorrow(); // Get next tommorrow weather
            doNextNextTommorrow(); // get next next tommorrow weather
            doGetCurrentTemp(); // Get current temp
            doGetDayAndTime(); 
        } // Setting all config for app
        void SettingUpfont()  // Setting font
        {
            daTxtLocation.Font = clsSetting.CustomFontStyle();
            lblLocation.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            lblUpdateTime.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);

            cbStartUpWithWindows.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            cb5Min.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            cb10Min.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            cb20Min.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            daTxtLocation.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            lblDay2.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            lblDay3.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            lblDay4.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            lblHighD2.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            lblHighD3.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            lblHighD4.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            lblLowD2.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            lblLowD3.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);
            lblLowD4.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.CustomFontSize);

            lblCurrentDay.Font = new Font(clsSetting.CustomFontFamilly, clsSetting.HeadingFontSize);

            lblCurrentDay.Text = clsDateTimeExtension.DayOfCurrent();
        }
        void SettingUp() // Setting up system
        {
            clsSystem system = new clsSystem();
            clsSetting setUp = new clsSetting();
            system.Collection = setUp.SetUp().Collection;
            system.StartUp = setUp.SetUp().StartUp;
            system.UpdateTime = setUp.SetUp().UpdateTime;
            system.Location = setUp.SetUp().Location;

            daTxtLocation.Text = system.Location;
            switch (system.UpdateTime)
            { 
                case 5:
                    cb5Min.Checked = true;
                    break;
                case 10:
                    cb10Min.Checked = true;
                    break;
                case 20:
                    cb20Min.Checked = true;
                    break;
            }
            if (system.StartUp == true)
            {
                cbStartUpWithWindows.Checked = true;
            }
            else { cbStartUpWithWindows.Checked = false; }
        }
        void FontExist() // Check font needed on client machine
        {
            string fontName = "Arial";
            Font testFont = new Font(fontName, 10.0f, FontStyle.Regular, GraphicsUnit.Pixel);

            if (testFont.Name == fontName)
            {
                // The font exists, so use it.
                this.Font = testFont;
            }
            else
            {
                // The font we tested doesn't exist, so fallback to Times.
                // this.Font = new Font("Times New Roman", 16.0f,FontStyle.Regular, GraphicsUnit.Pixel);
                if (System.Windows.Forms.MessageBox.Show("Máy tính của bạn chưa cài đặt font chữ Arial. Hãy cài đặt nó để chạy chương trình. Cảm ơn.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    Application.Exit();
                }

            }

        }
        #endregion ---

        #region --- Day
        void doGetDayAndTime()
        {
            lblDay2.Text = clsDateTimeExtension.DayOfTommorrow();
            lblDay3.Text = clsDateTimeExtension.DayOfNextTommorrow();
            lblDay4.Text = clsDateTimeExtension.DayOfNextNextTommorrow();

            lblHighD2.Text = clsWeather.HighInTommorrow() + "°C";
            lblLowD2.Text = clsWeather.LowInTommorrow() + "°C";

            lblHighD3.Text = clsWeather.HighInNextTommorrow() + "°C";
            lblLowD3.Text = clsWeather.LowInNextTommorrow() + "°C";

            lblHighD4.Text = clsWeather.HighInNextNextTommorrow() + "°C";
            lblLowD4.Text = clsWeather.LowInNextNextTommorrow() + "°C";
        }

        #endregion

        #region --- Temp
        void doGetCurrentTemp()
        {
            clsSystem sys = new clsSystem();
            clsSetting sett = new clsSetting();
            sys.Location = sett.SetUp().Location;

            daLBC.Text = clsWeather.CurrentTempInC(sys.Location).ToString() + "°C";
            daLBF.Text = clsWeather.CurrentTempInF(sys.Location).ToString() + "°F";
        }
        #endregion --- Temp

        #region --- Get weather status and replace icons
        void doTommorrowWeather()
        {
            clsSetting sett = new clsSetting();
            clsSystem sys = new clsSystem();
            sys.Location = sett.SetUp().Location;

            clsTick tick = new clsTick();
            switch(clsWeather.TommorowWeather(sys.Location))
            {
                /* Cloud icons */
                case "Cloudy":
                    picTomorrow.Image = tick.Cloudy2;
                    break;
                case "Partly Cloudy":
                    picTomorrow.Image = tick.Cloudy3;
                    break;
                case "Mostly Cloudy":
                    picTomorrow.Image = tick.Cloudy5;
                    break;
                /* Sunny icons*/
                case "Sunny":
                    picTomorrow.Image = tick.Sunny;
                    break;
                case "Partly Sunny":
                    picTomorrow.Image = tick.Sunny;
                    break;
                case "Mostly Sunny":
                    picTomorrow.Image = tick.Sunny;
                    break;
                /* Fog */
                case "Fog":
                    picTomorrow.Image = tick.Fog;
                    break;
                /* Hail */
                case "Hail":
                    picTomorrow.Image = tick.Hail;
                    break;
                /* Haze */
                case "Haze":
                    picTomorrow.Image = tick.Cloudy5;
                    break;
                /* Snow icons*/
                case "Change of Snow":
                    picTomorrow.Image = tick.Snow1;
                    break;
                case "Snow":
                    picTomorrow.Image = tick.Snow2;
                    break;
                case "Light Snow":
                    picTomorrow.Image = tick.Snow3;
                    break;
                case "Flurries":
                    picTomorrow.Image = tick.Snow4;
                    break;
                case "Rain and Snow":
                    picTomorrow.Image = tick.Sleet;
                    break;
                case "Icy":
                    picTomorrow.Image = tick.Snow5;
                    break;
                /* Mist */
                case "Mist":
                    picTomorrow.Image = tick.Mist;
                    break;
                /* Rain icons*/
                case "Rain":
                    picTomorrow.Image = tick.Shower1;
                    break;
                case "Light Rain":
                    picTomorrow.Image = tick.Light_Rain;
                    break;
                /* Clear */
                case "Clear":
                    picTomorrow.Image = tick.Cloudy1;
                    break;
                /* Storm */
                case "Thunderstorm":
                    picTomorrow.Image = tick.TStorm2;
                    break;
                case "Chance of Storm":
                    picTomorrow.Image = tick.TStorm1;
                    break;
                case "Storm":
                    picTomorrow.Image = tick.TStorm3;
                    break;
                /*Overcast*/
                case "Overcast":
                    picTomorrow.Image = tick.OverCast;
                    break;
                /*Smoke*/
                case "Smoke":
                    picTomorrow.Image = tick.OverCast;
                    break;
                default:
                    picTomorrow.Image = null;
                    break;
            }
        }
        void doCurrentWeather()
        {
            clsSetting sett = new clsSetting();
            clsSystem sys = new clsSystem();
            sys.Location = sett.SetUp().Location;

            clsTick tick = new clsTick();

            if (clsDateTimeExtension.CurrentDateTime().Equals("Day")) // Check if day
            { 
                switch(clsWeather.CurrentWeather(sys.Location))
                {
                    /* Cloudy icons*/
                    case "Cloudy":
                        picCurrentWeather.Image = tick.Cloudy1;
                       break;
                    case "Partly Cloudy":
                        picCurrentWeather.Image = tick.Cloudy3;
                        break;
                    case "Mostly Cloudy":
                        picCurrentWeather.Image = tick.Cloudy5;
                        break;
                    /* Sunny icons*/
                    case "Sunny":
                        picCurrentWeather.Image = tick.Sunny;
                        break;
                    case "Partly Sunny":
                        picCurrentWeather.Image = tick.Sunny;
                        break;
                    case "Mostly Sunny":
                        picCurrentWeather.Image = tick.Sunny;
                        break;
                    /* Fog */
                    case "Fog":
                        picCurrentWeather.Image = tick.Fog;
                        break;
                    /* Hail */
                    case "Hail":
                        picCurrentWeather.Image = tick.Hail;
                        break;
                    /* Haze */
                    case "Haze":
                        picCurrentWeather.Image = tick.Cloudy5;
                        break;
                    /* Snow icons*/
                    case "Change of Snow":
                        picCurrentWeather.Image = tick.Snow1;
                        break;
                    case "Snow":
                        picCurrentWeather.Image = tick.Snow2;
                        break;
                    case "Light Snow":
                        picCurrentWeather.Image = tick.Snow3;
                        break;
                    case "Flurries":
                        picCurrentWeather.Image = tick.Snow4;
                        break;
                    case "Rain and Snow":
                        picCurrentWeather.Image = tick.Sleet;
                        break;
                    case "Icy":
                        picCurrentWeather.Image = tick.Snow5;
                        break;
                    /* Mist */
                    case "Mist":
                        picCurrentWeather.Image = tick.Mist;
                        break;
                    /* Rain icons*/
                    case "Change of Rain":
                        picCurrentWeather.Image = tick.Shower1;
                        break;
                    case "Rain":
                        picCurrentWeather.Image = tick.Shower1;
                        break;
                    case "Light Rain":
                        picCurrentWeather.Image = tick.Light_Rain;
                        break;
                    /* Clear */
                    case "Clear":
                        picCurrentWeather.Image = tick.Cloudy1;
                        break;
                    /* Storm */
                    case "Thunderstorm":
                        picCurrentWeather.Image = tick.TStorm2;
                        break;
                    case "Chance of Storm":
                        picCurrentWeather.Image = tick.TStorm1;
                        break;
                    case "Storm":
                        picCurrentWeather.Image = tick.TStorm3;
                        break;
                    /*Overcast*/
                    case "Overcast":
                        picCurrentWeather.Image = tick.OverCast;
                        break;
                    /*Smoke*/
                    case "Smoke":
                        picCurrentWeather.Image = tick.OverCast;
                        break;
                    default:
                        picCurrentWeather.Image = null;
                        break;
                }
            }
            else if (clsDateTimeExtension.CurrentDateTime().Equals("Night"))
            {
                switch (clsWeather.CurrentWeather(sys.Location))
                {
                    /* Cloudy icons*/
                    case "Cloudy":
                        picCurrentWeather.Image = tick.Cloudy1Night;
                        break;
                    case "Partly Cloudy":
                        picCurrentWeather.Image = tick.Cloudy2Night;
                        break;
                    case "Mostly Cloudy":
                        picCurrentWeather.Image = tick.Cloudy3Night;
                        break;
                    /* Fog */
                    case "Fog":
                        picCurrentWeather.Image = tick.FogNight;
                        break;
                    /* Mist */
                    case "Mist":
                        picCurrentWeather.Image = tick.MistNight;
                        break;
                    /* Snow icons*/
                    case "Change of Snow":
                        picCurrentWeather.Image = tick.Snow1Night;
                        break;
                    case "Snow":
                        picCurrentWeather.Image = tick.Snow2Night;
                        break;
                    case "Light Snow":
                        picCurrentWeather.Image = tick.Snow3Night;
                        break;
                    case "Flurries":
                        picCurrentWeather.Image = tick.Snow4;
                        break;
                    case "Rain and Snow":
                        picCurrentWeather.Image = tick.Sleet;
                        break;
                    case "Icy":
                        picCurrentWeather.Image = tick.Snow5;
                        break;
                    /* Storm */
                    case "Thunderstorm":
                        picCurrentWeather.Image = tick.Strom2Night;
                        break;
                    case "Chance of Storm":
                        picCurrentWeather.Image = tick.Strom1Night;
                        break;
                    case "Storm":
                        picCurrentWeather.Image = tick.TStorm3;
                        break;
                    /* Rain icons*/
                    case "Rain":
                        picCurrentWeather.Image = tick.Shower2Night;
                        break;
                    case "Light Rain":
                        picCurrentWeather.Image = tick.Shower2Night;
                        break;
                    case "Change of Rain":
                        picCurrentWeather.Image = tick.Shower1Night;
                        break;
                    /* Hail */
                    case "Hail":
                        picCurrentWeather.Image = tick.Hail;
                        break;
                    /*Overcast*/
                    case "Overcast":
                        picCurrentWeather.Image = tick.OverCast;
                        break;
                    /*Smoke*/
                    case "Smoke":
                        picCurrentWeather.Image = tick.OverCast;
                        break;
                    default:
                        picCurrentWeather.Image = null;
                        break;
                }
            }
            
            
        }
        void doNextTommorrow()
        {
            clsSetting sett = new clsSetting();
            clsSystem sys = new clsSystem();
            sys.Location = sett.SetUp().Location;

            clsTick tick = new clsTick();
            switch (clsWeather.Day2Weather(sys.Location))
            {
                /* Cloud icons */
                case "Cloudy":
                    picNextTomorrow.Image = tick.Cloudy2;
                    break;
                case "Partly Cloudy":
                    picNextTomorrow.Image = tick.Cloudy3;
                    break;
                case "Mostly Cloudy":
                    picNextTomorrow.Image = tick.Cloudy5;
                    break;
                /* Sunny icons*/
                case "Sunny":
                    picNextTomorrow.Image = tick.Sunny;
                    break;
                case "Partly Sunny":
                    picNextTomorrow.Image = tick.Sunny;
                    break;
                case "Mostly Sunny":
                    picNextTomorrow.Image = tick.Sunny;
                    break;
                /* Fog */
                case "Fog":
                    picNextTomorrow.Image = tick.Fog;
                    break;
                /* Hail */
                case "Hail":
                    picNextTomorrow.Image = tick.Hail;
                    break;
                /* Haze */
                case "Haze":
                    picNextTomorrow.Image = tick.Cloudy5;
                    break;
                /* Snow icons*/
                case "Change of Snow":
                    picNextTomorrow.Image = tick.Snow1;
                    break;
                case "Snow":
                    picNextTomorrow.Image = tick.Snow2;
                    break;
                case "Light Snow":
                    picNextTomorrow.Image = tick.Snow3;
                    break;
                case "Flurries":
                    picNextTomorrow.Image = tick.Snow4;
                    break;
                case "Rain and Snow":
                    picNextTomorrow.Image = tick.Sleet;
                    break;
                case "Icy":
                    picNextTomorrow.Image = tick.Snow5;
                    break;
                /* Mist */
                case "Mist":
                    picNextTomorrow.Image = tick.Mist;
                    break;
                /* Rain icons*/
                case "Rain":
                    picNextTomorrow.Image = tick.Shower1;
                    break;
                case "Light Rain":
                    picNextTomorrow.Image = tick.Light_Rain;
                    break;
                /* Clear */
                case "Clear":
                    picNextTomorrow.Image = tick.Cloudy1;
                    break;
                /* Storm */
                case "Thunderstorm":
                    picNextTomorrow.Image = tick.TStorm2;
                    break;
                case "Chance of Storm":
                    picNextTomorrow.Image = tick.TStorm1;
                    break;
                case "Storm":
                    picNextTomorrow.Image = tick.TStorm3;
                    break;
                /*Overcast*/
                case "Overcast":
                    picNextTomorrow.Image = tick.OverCast;
                    break;
                /*Smoke*/
                case "Smoke":
                    picNextTomorrow.Image = tick.OverCast;
                    break;
                default:
                    picNextTomorrow.Image = null;
                    break;
            }
        }
        void doNextNextTommorrow()
        { 
             clsSetting sett = new clsSetting();
            clsSystem sys = new clsSystem();
            sys.Location = sett.SetUp().Location;

            clsTick tick = new clsTick();
            switch (clsWeather.Day3Weather(sys.Location))
            {
                /* Cloud icons */
                case "Cloudy":
                    picNextNextTomorrow.Image = tick.Cloudy2;
                    break;
                case "Partly Cloudy":
                    picNextNextTomorrow.Image = tick.Cloudy3;
                    break;
                case "Mostly Cloudy":
                    picNextNextTomorrow.Image = tick.Cloudy5;
                    break;
                /* Sunny icons*/
                case "Sunny":
                    picNextNextTomorrow.Image = tick.Sunny;
                    break;
                case "Partly Sunny":
                    picNextNextTomorrow.Image = tick.Sunny;
                    break;
                case "Mostly Sunny":
                    picNextNextTomorrow.Image = tick.Sunny;
                    break;
                /* Fog */
                case "Fog":
                    picNextNextTomorrow.Image = tick.Fog;
                    break;
                /* Hail */
                case "Hail":
                    picNextNextTomorrow.Image = tick.Hail;
                    break;
                /* Haze */
                case "Haze":
                    picNextNextTomorrow.Image = tick.Cloudy5;
                    break;
                /* Snow icons*/
                case "Change of Snow":
                    picNextNextTomorrow.Image = tick.Snow1;
                    break;
                case "Snow":
                    picNextNextTomorrow.Image = tick.Snow2;
                    break;
                case "Light Snow":
                    picNextNextTomorrow.Image = tick.Snow3;
                    break;
                case "Flurries":
                    picNextNextTomorrow.Image = tick.Snow4;
                    break;
                case "Rain and Snow":
                    picNextNextTomorrow.Image = tick.Sleet;
                    break;
                case "Icy":
                    picNextNextTomorrow.Image = tick.Snow5;
                    break;
                /* Mist */
                case "Mist":
                    picNextNextTomorrow.Image = tick.Mist;
                    break;
                /* Rain icons*/
                case "Rain":
                    picNextNextTomorrow.Image = tick.Shower1;
                    break;
                case "Light Rain":
                    picNextNextTomorrow.Image = tick.Light_Rain;
                    break;
                /* Clear */
                case "Clear":
                    picNextNextTomorrow.Image = tick.Cloudy1;
                    break;
                /* Storm */
                case "Thunderstorm":
                    picNextNextTomorrow.Image = tick.TStorm2;
                    break;
                case "Chance of Storm":
                    picNextNextTomorrow.Image = tick.TStorm1;
                    break;
                case "Storm":
                    picNextNextTomorrow.Image = tick.TStorm3;
                    break;
                /*Overcast*/
                case "Overcast":
                    picNextNextTomorrow.Image = tick.OverCast;
                    break;
                /*Smoke*/
                case "Smoke":
                    picNextNextTomorrow.Image = tick.OverCast;
                    break;
                default:
                    picNextNextTomorrow.Image = null;
                    break;
            }
        }
        #endregion ---

        private void picBSearchLocation_MouseHover(object sender, EventArgs e)
        {
            picBSearchLocation.Image = imgListSearchMouseHover.Images[0];
        }
        private void picBSearchLocation_MouseLeave(object sender, EventArgs e)
        {
            picBSearchLocation.Image = imgListSearchMouseHover.Images[1];
        }
        private void vbOk_Click(object sender, EventArgs e)
        {
           
        }
        private void tCurrentWeather_Tick(object sender, EventArgs e)
        {
            doGetCurrentTemp();
        }
    }
}

